window.fbAsyncInit = function() {
    FB.init({
        appId:'357686074337046' ,
        cookie: true,
        xfbml: true,
        oauth: true
    });
    
    FB.Event.subscribe('auth.login', function(response) {
        if (response.status == 'connected') {
            uid = response.authResponse.userID;
            
            FB.api('/'+uid, function(user) {
                if (user) {
                   
                    var par="username="+user.username+"&nome="+user.first_name+"&cognome="+user.last_name+"&eMail="+user.email;
                    document.location.href="/EnterpriseApplication12-13-war/Controller?button=login&type=fb&"+par;
                }
            });
        } else if (response.status == 'not_authorized') {
           
        } else {
           
        }
    });
    
};
  
function logoutFB(){
    FB.logout();
}
      
      
function loginFB(){
    FB.login(null,{scope: 'email'});
}
// Load the SDK Asynchronously
(function(d){
    var js, id = 'facebook-jssdk';
    if (d.getElementById(id)) {
        return;
    }
    js = d.createElement('script');
    js.id = id;
    js.async = true;
    js.src = "//connect.facebook.net/en_US/all.js";
    // ref.parentNode.insertBefore(js, ref);
    d.getElementsByTagName('head')[0].appendChild(js);
}(document));


(function() {
    var po = document.createElement('script');
    po.type = 'text/javascript';
    po.async = true;
    po.src = 'https://apis.google.com/js/client:plusone.js';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(po, s);
})();
     
     
function loginGoogle(){
    var config = {
        client_id: "218328793392.apps.googleusercontent.com",
        scope: "https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email"
    };
    gapi.auth.authorize(config,signinCallback);
}

function logoutGoogle(){
    document.location.href="https://accounts.google.com/logout";
}
function signinCallback(authResult) {
    var email;
    var id;
    var username;
    var name;
    var cognome;
    if (authResult['access_token']) {
        gapi.client.load('oauth2', 'v2', function() {
            gapi.client.oauth2.userinfo.get().execute(function(resp) {
                
                email=resp.email;
                id=resp.id;
                gapi.client.load('plus','v1', function(){
                    var request = gapi.client.plus.people.get({
                        'userId': id
                    });
                    request.execute(function(resp) {
                        
                        cognome=resp.name.familyName;
                        name=resp.name.givenName;
                        username=name+"."+cognome;
                        var par="username="+username+"&nome="+name+"&cognome="+cognome+"&eMail="+email;
                        document.location.href="/EnterpriseApplication12-13-war/Controller?button=login&type=google&"+par;
                    })
                });
            })
        });

        
    } else if (authResult['error']) {
    // There was an error.
    // Possible error codes:
    //   "access_denied" - User denied access to your app
    //   "immediate_failed" - Could not automatically log in the user
    // console.log('There was an error: ' + authResult['error']);
    }
}
